package com.bsj.sipgateway.mq.producer;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bsj.power.common.config.rocketmq.producer.BaseMqPushUtil;
import com.bsj.sipgateway.cache.VehicleCache;
import com.bsj.sipgateway.command.RocketMqCommand;
import com.bsj.sipgateway.common.constant.Constants;
import com.bsj.sipgateway.common.def.CameraDto;
import com.bsj.sipgateway.mysql.report.commandissued.entity.Commandissued;
import com.bsj.sipgateway.mysql.report.commandissued.service.ICommandissuedService;
import com.bsj.sipgateway.mysql.report.commandissued.service.impl.CommandissuedServiceImpl;
import com.bsj.sipgateway.sip.bean.VehicleInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author WPH
 * @Description 命令响应
 * @Date 2022/8/8 19:36
 */
@Component
@Slf4j
public class CommandUpProducer {

    // 命令下发应答会话
    private static String commandUpTopic = "TOPIC-BSJ-COMMAND-UP";

    // 车辆基础信息topic
    private static String vehicleBaseInfoTopic = "VEHICLE-BASE-INFO-TOPIC";

    // 命令下发应答Tag
    private static String cmdUpTag = "";

    //修改摄像头信息tag
    private static String updateCamera = "updateCamera";

    @Autowired
    private CommandissuedServiceImpl commandissuedService;

    @Autowired
    private BaseMqPushUtil baseMqPushUtil;

    /**
     * 发布数据
     *
     * @param command mq响应数据
     */
    public void send(RocketMqCommand command, String sqlType) {
        try {
            String jsonStr = JSON.toJSONString(command);
            baseMqPushUtil.sendTopicBytes(commandUpTopic, cmdUpTag, jsonStr.getBytes());
            log.info("命令下发应答:{}", jsonStr);
            if (Constants.SQL_INSERT.equals(sqlType)) {
                //新增
                VehicleInfo vehicle = VehicleCache.get(command.getDevice());
                if (vehicle != null) {
                    Commandissued commandissued = new Commandissued(command.getUuid(), vehicle.getPlate(),
                            vehicle.getVehicleId(), vehicle.getGroupId(), command.getUserName(),
                            command.getCmdMessage(), command.getCmdType());
                    if (command.getStatus() == 0) {
                        commandissued.setResult("FAIL");
                        commandissued.setResultMsg(command.getRetMsg());
                    } else if (command.getStatus() == 1) {
                        commandissued.setResult("SUCCESS");
                        commandissued.setResultMsg("命令下发成功");
                    }
                    commandissuedService.replaceInto(commandissued);
                }
            } else {
                //更新
                UpdateWrapper<Commandissued> wrapper = new UpdateWrapper<>();
                if (command.getStatus() == 0) {
                    wrapper.set("result", "FAIL");
                    wrapper.set("resultMsg", command.getRetMsg());
                } else {
                    wrapper.set("result", "SUCCESS");
                    wrapper.set("resultMsg", "命令下发成功");
                }
                wrapper.eq("uuid", command.getUuid());
//                commandissuedService.update(wrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("命令下发应答发布失败：");
        }
    }

    public void send(CameraDto cameraDto) {
        try {
            String jsonStr = JSON.toJSONString(cameraDto);
            baseMqPushUtil.sendTopicBytes(vehicleBaseInfoTopic, updateCamera, jsonStr.getBytes());
            log.info("发送摄像头信息修改mq:{}", jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送摄像头信息修改失败：");
        }
    }


}
